﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace CRMAdminDashboard
{
    public partial class FormShowText : Form
    {

        #region Properties

        /// <summary>
        /// File name (if a file was written, otherwise null)
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// Displayed text
        /// </summary>
        public string OriginalText { get; set; }

        /// <summary>
        /// Text is XML?
        /// </summary>
        public bool IsXml { get; set; }

        #endregion

        #region Entry Points

        /// <summary>
        /// Standard ctor.
        /// </summary>
        /// <param name="text"></param>
        /// <param name="encoding"></param>
        public FormShowText(string text, bool isXml)
        {
            InitializeComponent();
            txtText.WordWrap = cbWordWrap.Checked;
            this.OriginalText = text;
            txtText.Text = OriginalText;
            this.IsXml = isXml;
        }

        #endregion

        #region Events

        private void cbWordWrap_CheckedChanged(object sender, EventArgs e)
        {
            txtText.WordWrap = cbWordWrap.Checked;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            if (IsXml)
            {
                dialog.Filter = "XML files (*.xml)|*.xml|All files (*.*)|*.*";
            }
            else
            {
                dialog.Filter = "TXT files (*.txt)|*.txt|All files (*.*)|*.*";
            }
            dialog.OverwritePrompt = true;

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                StreamWriter file = null;
                try
                {
                    file = new StreamWriter(dialog.FileName, false, Encoding.UTF8);
                    file.Write(OriginalText);
                    FileName = dialog.FileName;
                }
                finally
                {
                    if (file != null)
                    {
                        file.Close();
                    }
                }
            }
        }

        #endregion

    }
}
